<!--
 * @Author: Zhang ShiWei
 * @Date: 2022/5/9 16:15
 * @LastEditTime: 2021-04-14 14:10:38
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: exportDataToExcel
 * @FileName: exportDataToExcel.vue
-->
<template>
  <el-button size="mini" icon="el-icon-download" @click="getData">
    {{ fileName }}
  </el-button>
</template>

<script>
import FileSaver from "file-saver";
import XLSX from "xlsx";
export default {
  name: "ExportDataToExcel",
  props: {
    fileName: {
      type: String,
      default: function() {
        return "导出模板";
      }
    },
    downName: {
      type: String,
      default: function() {
        return "导出";
      }
    }
  },
  data() {
    return {};
  },
  watch: {},
  created() {},
  mounted() {},
  methods: {
    getData() {
      this.$emit("get-data");
    },
    getExcel(tableData, downName) {
      /* // 从头开始创建工作簿
      var wb = XLSX.utils.book_new();
      /!* Initial row *!/
      var ws = XLSX.utils.json_to_sheet(
        [{ A: "S", B: "h", C: "e", D: "e", E: "t", F: "J", G: "S" }],
        { header: ["A", "B", "C", "D", "E", "F", "G"], skipHeader: true }
      );

      /!* Write data starting at A2 *!/
      XLSX.utils.sheet_add_json(
        ws,
        [
          { A: 1, B: 2 },
          { A: 2, B: 3 },
          { A: 3, B: 4 }
        ],
        { skipHeader: true, origin: "A2" }
      );

      /!* Write data starting at E2 *!/
      XLSX.utils.sheet_add_json(
        ws,
        [
          { A: 5, B: 6, C: 7 },
          { A: 6, B: 7, C: 8 },
          { A: 7, B: 8, C: 9 }
        ],
        { skipHeader: true, origin: { r: 1, c: 4 }, header: ["A", "B", "C"] }
      );

      /!* Append row *!/
      XLSX.utils.sheet_add_json(
        ws,
        [{ A: 4, B: 5, C: 6, D: 7, E: 8, F: 9, G: 0 }],
        {
          header: ["A", "B", "C", "D", "E", "F", "G"],
          skipHeader: true,
          origin: -1
        }
      );
      XLSX.utils.book_append_sheet(wb, ws, "sheet");
      XLSX.utils.book_append_sheet(wb, ws, "sheet2");
      let wopts = {
        bookType: "xlsx",
        bookSST: true,
        type: "binary",
        cellStyles: true
      };
      var wbout = XLSX.write(wb, wopts);
      FileSaver.saveAs(
        new Blob([s2ab(wbout)], { type: "application/octet-stream" }),
        "使用情况.xlsx"
      );*/
      // 创建workbook对象
      const wb = XLSX.utils.book_new();
      for (var key in tableData) {
        // 把json转为worksheet对象
        var ws;
        if (tableData[key].header !== undefined) {
          ws = XLSX.utils.json_to_sheet(tableData[key].data, {
            header: tableData[key].header
          });
        } else {
          ws = XLSX.utils.json_to_sheet(tableData[key].data);
        }
        // 添加worksheet 到 workbook
        XLSX.utils.book_append_sheet(wb, ws, tableData[key].sheetName);
      }
      // 写出 arraybuffer 数据
      const wb_out = XLSX.write(wb, { bookType: "xlsx", type: "array" });
      // 构建Blob对象
      const _blob = new Blob([wb_out], { type: "application/octet-stream" });
      // 下载
      FileSaver(_blob, downName + ".xlsx");
    },
    // 字符串转ArrayBuffer
    s2ab(s) {
      var buf = new ArrayBuffer(s.length);
      var view = new Uint8Array(buf);
      for (var i = 0; i !== s.length; ++i) view[i] = s.charCodeAt(i) & 0xff;
      return buf;
    }
  }
};
</script>

<style scoped lang="scss">
</style>
